package com.server.server_a10.sqlTest;

import com.server.server_a10.entity.Home;
import com.server.server_a10.entity.Room;
import com.server.server_a10.entity.User;
import com.server.server_a10.entity.lightingSystem.Light;
import com.server.server_a10.mapper.HomeMapper;
import com.server.server_a10.mapper.LightMapper;
import com.server.server_a10.mapper.RoomMapper;
import com.server.server_a10.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class sqlControllerTest {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LightMapper lightMapper;
    @Autowired
    private HomeMapper homeMapper;

    @Autowired
    private RoomMapper roomMapper;

    @GetMapping(value = "/test_getAllUser")
    public void getAllUser(){
        List<User> userList = userMapper.selectList(null);
        for (User u:userList){
            System.out.println(u.toString());
        }
        return;
    }

    @GetMapping(value = "/test_getUserById")
    public String getUserById(int id){
        User u = userMapper.selectById(id);
        return u.toString();
    }

    @GetMapping(value = "/test_updateUserById")
    public boolean updateUserById(int id,String name){
        User user = new User(id,name);
        int rows = userMapper.updateById(user);
        return rows>0;
    }

    @GetMapping(value = "/test_getHomeById")
    public Home getHomeById(@RequestBody Map<String, Object> request){
        int id = (int) request.get("id");
        Home home = homeMapper.selectById(id);
        return home;
    }


    /**
     * 获取用户所绑定的房子
     * @param id
     * @return homes
     */
    @GetMapping(value = "/test_getHomeByUserId")
    public List<Home> getHomeByUserId(int id){
        List<Home> homes = homeMapper.getHomeByUserId(id);
        return homes;
    }

    @GetMapping(value = "/test_getRoomByHomeId")
    public List<Room> getRoomByHomeId(int id){
        List<Room> rooms = roomMapper.getRoomByHomeId(id);
        return rooms;
    }

    @GetMapping(value = "/test_getLightByRoomId")
    public List<Light> getLightByRoomId(int id){
        List<Light> lights = lightMapper.getLightByRoomId(id);
        return lights;
    }
}
